package Homework2

import scala.io.StdIn

object Game {
  var user: User = _
  var computer: Computer = _
  var turn: Int = 0
  val winOrLoss: Array[Array[String]] = Array(Array("draw", "loss", "win"),
                                              Array("win", "draw", "loss"),
                                              Array("loss", "win", "draw"))

  def main(args: Array[String]): Unit = {
    println("------------------------欢迎进入游戏世界--------------------------")
    println("*******************************")
    println("**********猜拳，开始************")
    println("*******************************")

    val user: User = new User
    val computer: Computer = new Computer
    println("请选择对战角色：（1.刘备   2.关羽   3.张飞）")
    StdIn.readInt() match {
      case 1 => println("你选择了与刘备对战"); computer.setName("刘备")
      case 2 => println("你选择了与关羽对战"); computer.setName("关羽")
      case 3 => println("你选择了与张飞对战"); computer.setName("张飞")
    }
    println("要开始么?y/n")
    if(StdIn.readChar() == 'n') {
      println("退出游戏")
      System.exit(0)
    }

    while (true){
      println("请出拳！1剪刀 2石头 3布")
      val userFist = user.showFist()
      println(computer.getName + "出拳!")
      val computerFist = computer.showFist()
      val result = winOrLoss(userFist - 1)(computerFist - 1)
      turn += 1
      result match {
        case "win" => println("恭喜，你赢了")
          user.setWin(user.getWin + 1)
          computer.setLoss(computer.getLoss + 1)
        case "loss" => println("很遗憾，你输了")
          user.setLoss(user.getLoss + 1)
          computer.setWin(computer.getWin + 1)
        case "draw" => println("和局，下次继续努力")
          user.setDraw(user.getDraw + 1)
          computer.setDraw(computer.getDraw + 1)
      }
      println("是否开启下一轮？y/n")
      if(StdIn.readChar() == 'n') {
        println("退出游戏")
        println(computer.getName + "VS" + user.getName)
        println("对战次数" + turn +"次")
        println("姓名 等分 胜局 和局 负局")
        for (i <- Tuple2(user, computer).productIterator){
          println(i.toString)
        }
        println("--------------------------------------------")
        System.exit(0)
      }
    }
  }
}
